/**
 * Created by qmh on 2016/7/1.
 */

import React from 'react';
import {Form,Col,Input,Button,Collapse,Row,Modal,Select,Tree} from 'antd';
import util from 'util';
import {menus} from 'menu';

var FormItem=Form.Item;
var Panel=Collapse.Panel;
const Option = Select.Option;
const TreeNode = Tree.TreeNode;

@Form.create()
class Search extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            children:[]
        };
        console.log("search12");
    }
    componentDidMount() {
        console.log("search2");
        let list=[];

        // 获取到数据字典
        var diclist= util.getSysDict("roletype");
        for (let i = 0; i < diclist.length; i++) {
            list.push(<Option key={diclist[i].itemkey} >{diclist[i].itemval}</Option>);
        }
        this.setState({
            children:list
        });

        var {setFieldsValue} = this.props.form;
        // setFieldsValue( this.props.data);
        setFieldsValue({"rolename":"","roletype":"","descript":""});
    }
    reset(e){
        e.preventDefault();
        var {setFieldsValue} = this.props.form;
        setFieldsValue( {
            rolename:"",
            roletype:"",
            descript:""
        });
    }
    initsearch(e){
        //e.preventDefault();
        //console.log('接受到表单值',this.props.form.getFieldsValue());
        this.props.propsearch(this.props.form.getFieldsValue());
    }
    render(){
        const {getFieldProps} =this.props.form;
        const formItemLayout={
            labelCol:{span:8},
            wrapperCol:{span:14}
        };
        return(
          <Collapse defaultActiveKey={['1']}>
              <Panel header="角色信息设置查询" key="1">
                  <Form inline className="ant-advanced-search-form">
                      <Row>
                          <Col span={8}>
                              <FormItem label="角色名称：" {...formItemLayout}>
                                  <Input
                                      {...getFieldProps('rolename')}
                                  />
                              </FormItem>
                          </Col>
                          <Col span={8}>
                              <FormItem label="描述：" {...formItemLayout}>
                                  <Input
                                      {...getFieldProps('descript')}
                                  />
                              </FormItem>
                          </Col>
                      </Row>

                      <Row style={{textAlign:'center'}}>
                          <Button className="btnMarginRright" type="primary" htmlType="submit" onClick={this.initsearch.bind(this)}>搜索</Button>
                          <Button  htmlType="submit" onClick={this.reset.bind(this)}>重置</Button>
                      </Row>
                  </Form>
              </Panel>
          </Collapse>
        );
    }
}

@Form.create()
class Edit extends React.Component{
    getValidateStatus(field) {
        const { isFieldValidating, getFieldError, getFieldValue } = this.props.form;

        if (isFieldValidating(field)) {
            return 'validating';
        } else if (!!getFieldError(field)) {
            return 'error';
        } else if (getFieldValue(field)) {
            return 'success';
        }
    }
    constructor(props){
        super(props);
        this.state={
            children:[],
            setvalue:function(){},
            typechildren:[]
        };
    }
    componentDidMount() {
        console.log("角色moun");
        console.log(this.props.parentRole);
        let children = [];
        for (let i = 0; i < this.props.parentRole.length; i++) {
            children.push(<Option key={this.props.parentRole[i].roleid} >{this.props.parentRole[i].rolename}</Option>);
        }

        let list=[];
        var diclist= util.getSysDict("roletype");
        //console.log("角色类型");
        console.log(diclist);
        for (let i = 0; i < diclist.length; i++) {
            list.push(<Option key={diclist[i].itemkey} >{diclist[i].itemval}</Option>);
        }
        this.setState({
            children:children,
            typechildren:list
        });

        //let parentlist=[];
        var {setFieldsValue} = this.props.form;
        setFieldsValue( this.props.data);
    }

    render(){
        const formItemLayout={
            labelCol:{span:6},
            wrapperCol:{span:14}
        };

        const { getFieldProps, getFieldError, isFieldValidating } = this.props.form;
        const parentroleProps = getFieldProps('parentroleid', {
            rules: [
                { required: true, message: '请选择父角色' },
            ],
        });
        const rolenameProps = getFieldProps('rolename', {
            rules: [
                { required: true, min: 0, message: '用户名必须输入' },
            ],
        });
        //const roletypeProps = getFieldProps('roletype', {
        //    rules: [
        //        { required: true, min: 0, message: '请选择角色类型' },
        //    ]
        //});

        return(
            <Form horizontal form={this.props.form}>
                <FormItem label="父角色" {...formItemLayout}  hasFeedback
                  help={isFieldValidating('parentroleid') ? '校验中1...' : (getFieldError('parentroleid') || []).join(', ')}>
                    <Select  {...parentroleProps} style={{ width: 120 }} placeholder="请选择" disabled={this.props.operation=="edit"}>
                        {this.state.children}
                    </Select>
                </FormItem>
                <FormItem label="角色名称"
                    {...formItemLayout}
                      hasFeedback
                      help={isFieldValidating('rolename') ? '校验中...' : (getFieldError('rolename') || []).join(', ')}>
                    <Input {...rolenameProps} placeholder="请输入角色名称" />
                </FormItem>
                <FormItem label="描述" {...formItemLayout}>
                    <Input {...getFieldProps('descript')} type="textarea" rows="3"/>
                </FormItem>
            </Form>
        );
    }
}

class FuncTree extends React.Component{
    constructor(props){
        super(props);
        //const keys = this.props.keys;

        // console.log(menus);
        //  const keys= ;
        this.state={
            defaultExpandedKeys:this.props.selecteds,
            defaultSelectedKeys:this.props.selecteds,
            defaultCheckedKeys:this.props.selecteds,
            nodetree:[]
        };
    }
    onExpand(treeNode, expand, expandedKeys) {
        //console.log('onExpand', expand, expandedKeys);
    }
    onSelect(info) {
       // console.log('selected5', info);
    }
    onCheck(info) {
        //console.log('onCheck123', info);
        this.props.treeOnCheck(info);
    }
    getTreeDom(childlist){
        if(!childlist){
            return;
        }
        let list=[];

        for(let i=0 ;i<childlist.length;i++){
          //  console.log("treekey"+childlist[i].id);
            list.push(<TreeNode title={childlist[i].text} key={childlist[i].id} >
                {this.getTreeDom(childlist[i].children)}
            </TreeNode>);
        }
        return list;
    }
    render(){
        return(
            <Tree className="myCls" showLine multiple checkable
                  onExpand={this.onExpand}
                  defaultSelectedKeys={this.state.defaultSelectedKeys}
                  defaultCheckedKeys={this.state.defaultCheckedKeys}
                  onSelect={this.onSelect} onCheck={this.onCheck.bind(this)}>
                {this.getTreeDom(this.props.menudata)}
            </Tree>
        );
    }
}

export {Search,Edit,FuncTree}